/*
 * @Author: 
 * @Date: 2022-02-09 16:21:38
 * @LastEditors: lqj
 * @LastEditTime: 2022-02-10 14:33:49
 * @Description: redux -> reducer
 */
const initState = 0 // 初始化

const INCREASE_ACTION = 'add'
const REDUCE_ACTION = 'reduce'
const RIDE_ACTION = 'ride'
const BESIDE_ACTION = 'beside'
const CLEAR_ACTION = 'clear'

// 同步action，action的值返回一个object类型的一般对象
const createIncrease = data => ({ type: INCREASE_ACTION, data })
const createReduce = data => ({ type: REDUCE_ACTION, data })
const createRide = data => ({ type: RIDE_ACTION, data })
const createBeside = data => ({ type: BESIDE_ACTION, data })
const createClear = data => ({ type: CLEAR_ACTION, data })
// 异步action，action的值返回一个function函数，函数开启一个异步队列，非必须
// 异步action中一般都可以调用同步action
const createIncreaseAsync = (data, time) => {
  return (dispatch) => {
    setTimeout(() => {
      dispatch(createIncrease(data))
    }, time);
  }
}

function countRedux(pre = initState, action) {
  const { type, data } = action
  switch (type) {
    case INCREASE_ACTION:
      return pre + data
    case REDUCE_ACTION:
      return pre - data
    case RIDE_ACTION:
      return pre * data
    case BESIDE_ACTION:
      return pre / data
    case CLEAR_ACTION:
      return data
    default:
      return pre
  }
}

export {
  createIncrease,
  createReduce,
  createRide,
  createBeside,
  createIncreaseAsync,
  createClear,
  countRedux
}